package com.yanchangapp.util;
import android.annotation.SuppressLint;
import android.os.Build;
import android.view.View;
import android.webkit.WebSettings;
import android.webkit.WebView;
import android.widget.ZoomButtonsController;

/**
 * Created by apple on 2019/7/8.
 * description: webview帮助类
 */
public class WebviewHelper {

    public static final String linkCss =
            "<link rel=\"stylesheet\" type=\"text/css\" href=\"file:///android_asset/h5/css/common.css\">";
    public final static String WEB_STYLE = linkCss;

    // 初始化webview
    @SuppressLint({"JavascriptInterface", "SetJavaScriptEnabled"})
    public static void initWebView(WebView webView) {
        WebSettings settings = webView.getSettings();
        settings.setDefaultFontSize(14);
        settings.setJavaScriptEnabled(true);
        settings.setSupportZoom(false);
        settings.setBuiltInZoomControls(false);
        settings.setUseWideViewPort(true);
        settings.setLoadWithOverviewMode(false);
        settings.setCacheMode(WebSettings.LOAD_NO_CACHE);
        int sysVersion = Build.VERSION.SDK_INT;
        if (sysVersion >= 11) {
            settings.setDisplayZoomControls(false);
        } else {
            ZoomButtonsController zbc = new ZoomButtonsController(webView);
            zbc.getZoomControls().setVisibility(View.GONE);
        }
        //webView.setWebViewClient(UiUtil.getWebViewClient());
    }

    // 设置body
    public static String setHtmlCotentSupportImagePreview(String body) {
//        // 过滤掉 img标签的width,height属性
//        body = body.replaceAll("(<img[^>]*?)\\s+width\\s*=\\s*\\S+", "$1");
//        body = body.replaceAll("(<img[^>]*?)\\s+height\\s*=\\s*\\S+", "$1");
//        // 过滤table的内部属性
//        body = body.replaceAll("(<table[^>]*?)\\s+border\\s*=\\s*\\S+", "$1");
//        body = body.replaceAll("(<table[^>]*?)\\s+cellspacing\\s*=\\s*\\S+", "$1");
//        body = body.replaceAll("(<table[^>]*?)\\s+cellpadding\\s*=\\s*\\S+", "$1");
        return body;
    }


    // 头部字符串
    public static String getWebViewStartString(){
        return "<html> <head>  <link rel=\"stylesheet\" type=\"text/css\" href=\"file:///android_asset/h5/css/common.css\">\n";
    }

    // 内容开始了
    public static String getWebViewStart(){
        return "<script type=\"text/javascript\" src=\"file:///android_asset/h5/js/scale.js\"></script></head>";
    }

    // 普通的头部
    public static String normalViewBodyString(){
        return "<body style='background-color: #FFF'>" +
                "<div class='contentstyle' id='content' style='width:auto;'>";
    }

    public static String getWebViewEndString(){
        return "</div></body></html>";
    }


    // 转发的开始字符串....
    public static String getTransWVStartString(){
        return "<html>" +
                "<head>" +
                "<link rel=\"stylesheet\" type=\"text/css\" href=\"file:///android_asset/h5/css/write.css\">\n"+
                "<meta name=\"viewport\"\n" +
                "content=\"width=device-width,initial-scale=1.0,\n" +
                "minimum-scale=1.0,maximum-scale=1.0,user-scalable=no\">"+
                "<script type=\"text/javascript\" src=\"file:///android_asset/h5/js/trans.js\"></script></head>" +
                "<body>\n" +
                "<div id=\"editor\" style=\"border: none;outline: none;\">";
    }


    public static String getTransWVEndString(){
        return "</div></body></html>";
    }

}